using UnityEngine;
using Delta2DGame.Base;
using Delta2DGame.State;

/// <summary>
/// 敌人状态机控制器
/// </summary>
public class EnemyStateMachine : MonoBehaviour
{
    public EnemyState currentState { get; private set; }
    [SerializeField] private EnemyController enemy;

    // 状态实例
    public PatrolState patrolState { get; private set; }
    public AttackState attackState { get; private set; }
    public ReturnState returnState { get; private set; }

    private void Awake()
    {
        // 自动获取EnemyController组件
        if (enemy == null)
            enemy = GetComponent<EnemyController>();

        // 初始化所有状态
        patrolState = new PatrolState(this, enemy, "isPatrolling");
        attackState = new AttackState(this, enemy, "isAttacking");
        returnState = new ReturnState(this, enemy, "isReturning");
    }

    private void Start()
    {
        // 默认进入巡逻状态
        SwitchState(patrolState);
    }

    private void Update()
    {
        currentState?.Update();
    }

    private void FixedUpdate()
    {
        currentState?.FixedUpdate();
    }

    /// <summary>
    /// 切换状态
    /// </summary>
    public void SwitchState(EnemyState newState)
    {
        currentState?.Exit();
        currentState = newState;
        currentState?.Enter();
    }
}